interface TreeNodeBase<T = any> {
  id: string | number;
  children?: TreeNodeBase<T>[];
}

export function flattenTree<T extends TreeNodeBase>(treeData: T[]): Omit<T, "children">[] {
  const result: Omit<T, "children">[] = [];

  function flatten(node: T) {
    const { children, ...rest } = node;
    result.push(rest as Omit<T, "children">);
    if (children?.length) {
      children.forEach(child => flatten(child as T));
    }
  }

  treeData.forEach(node => flatten(node));
  return result;
}
